package com.wondersgroup.fjzlpt.org.dao.impl.yljgxx;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;

@Repository
public class YljgxxDAOImpl extends BaseJTDAOImpl implements YljgxxDAO{

	@Override
	@Autowired
	@Qualifier("TB_ZD_YLJGXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}
	
	@Autowired
	@Qualifier("TB_ZD_KSXX")
	private TableBean ksTable;

	@Value("${common.WSGLJGLBDM}")
	private String GLJGLBDM;
	
	@Override
	public JSONObject queryPage(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder();
		sbud.append("select * ");
		sbud.append(" from VIEW_YLJGXX a where 1=1 ");
		List list = new ArrayList();
		String value = map.get("YLJGMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.YLJGMC like ?");
			list.add("%"+value+"%");
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.PTJGID = ?");
			list.add(value);
		}
		value = map.get("XZQHDM");
		String CXSJ = map.get("CXSJ"); //查询上级
		if(!StringUtils.isEmpty(value) && !StringUtils.isEmpty(CXSJ))
		{
			sbud.append(" and ( ");
			int count = value.length()/2;
			List xzqhSqlList = new ArrayList(count);
			for(int i = 0 ; i < count ;i ++ ){
				String xzqh = value.substring(0 , (i+1)*2);
				xzqhSqlList.add(" (a.XZQHDM = ? and a.WSJGLBDM like ?) ");
				list.add(xzqh);
				list.add(GLJGLBDM+"%");
			}
			sbud.append(String.join(" or ", xzqhSqlList));
			//添加本机构id为条件
			sbud.append(" or a.PTJGID = ? ");
			list.add(value = map.get("YLJGID_this"));
			//是否管理机构（是：查询其下级）
			String JGLB = map.get("JGLB");
			if(JGLB != null && JGLB.indexOf(GLJGLBDM) != -1){
				sbud.append(" or a.XZQHDM like ?");
				list.add(map.get("XZQHDM")+"%");
			}
			sbud.append(" ) ");
		}
		else if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.XZQHDM like ?");
			list.add(value+"%");
		}
		value = map.get("YLJGIDS");
		if(!StringUtils.isEmpty(value))
		{
			
			sbud.append("and a.PTJGID not in (");
			String[] strs = value.split(",");
			String params = "";
			for (String str : strs) {
				params += "?,";
				list.add(str);
			}
			params = params.substring(0, params.lastIndexOf(","));
			sbud.append(params+")");
		}
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by a.XZQHDM, a.YLJGDM", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}

	
	@Override
	public JSONArray queryList(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder();
		sbud.append("select * ");
		sbud.append(" from VIEW_YLJGXX a where 1=1 ");
		List list = new ArrayList();
		String value = map.get("YLJGIDS");
		if(!StringUtils.isEmpty(value))
		{
			
			sbud.append("and a.PTJGID in (");
			String[] strs = value.split(",");
			String params = "";
			for (String str : strs) {
				params += "?,";
				list.add(str);
			}
			params = params.substring(0, params.lastIndexOf(","));
			sbud.append(params+")");
		}else{
			return new JSONArray();
		}
		SQLBean sb = new SQLBean();
		sb.setSql(sbud.toString());
		sb.setParamAry(list);
		return super.queryList(sb);
	}
	
	/**
	 * 查询科室信息
	 * @param list
	 */
	public JSONArray queryKsxx(List<String> list) {//tb_zd_ksxx
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select * from "+this.ksTable.getTableName()+" where PTJGID in (");
		String params = "";
		for (String string : list) {
			params += "?,";
		}
		params = params.substring(0, params.lastIndexOf(","));
		sbuf.append(params);
		sbuf.append(")");
		SQLBean sb = new SQLBean(sbuf.toString(), list.toArray());
		return super.queryList(sb);
	}

	/**
	 * 查询所有医疗机构信息
	 * @param map
	 * @return
	 */
	@Override
	public JSONArray queryAll(Map<String, String> map) {
		StringBuffer sqlBuf = new StringBuffer("select * from ").append(this.tb.getTableName());
		
		SQLBean bean = new SQLBean(sqlBuf.toString());
		
		return super.queryList(bean);
	}

}
